package cn.com.fuhuitong.main.home.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.view.View
import cn.com.fuhuitong.base.BaseDelegateAdapter
import cn.com.fuhuitong.main.base.RecyclerViewHolder
import cn.com.fuhuitong.main.home.entity.CharityListEntity
import com.alibaba.android.vlayout.LayoutHelper
import cn.com.fuhuitong.R
import cn.com.fuhuitong.app.BASE_HTTP_URL
import cn.com.fuhuitong.app.getImageUrl
import cn.com.fuhuitong.main.ItemViewType
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.TimeUtils
import com.bumptech.glide.Glide
import kotlinx.android.synthetic.main.item_charity_content.view.*
import kotlinx.android.synthetic.main.item_tab_layout.view.*

class CharityTabAdapter(
    private val mContext: Context,
    private val mHelper: LayoutHelper,
    private val tabData: MutableList<String>
) : BaseDelegateAdapter<String>(
    mContext = mContext,
    mHelper = mHelper,
    mLayoutId = R.layout.item_tab_layout,
    mItemViewType = ItemViewType.CHARITY_TAB
) {

    var selectedPosition = 0
        set(value) {
            if (field != value) {
                field = value
                notifyDataSetChanged()
            }
        }

    init {
        setAddDataNew(tabData)
    }

    override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
        super.onBindViewHolder(holder, position)
        holder.itemView.text_tab_layout_name.text = data[position]
        holder.itemView.text_tab_layout_name.setTextColor(
            mContext.resources.getColor(
                if (holder.layoutPosition == selectedPosition) R.color.colorMain else R.color.colorMainGrey
            )
        )
        holder.itemView.isSelected = holder.layoutPosition == selectedPosition
    }
}

class CharityListAdapter(
    private val mContext: Context, private val mHelper: LayoutHelper
) : BaseDelegateAdapter<CharityListEntity>(
    mContext = mContext,
    mHelper = mHelper,
    mLayoutId = R.layout.item_charity_content,
    mItemViewType = ItemViewType.CHARITY_LIST
) {
    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(itemView: View, position: Int, itemData: CharityListEntity) {
        super.onBindViewHolder(itemView, position, itemData)

        Glide.with(itemView).load(getImageUrl(itemData.image))
            .into(itemView.text_charity_icon)

        itemView.text_charity_name.text = itemData.title
        itemView.text_charity_address.text =
            "地址：${itemData.address}"
        itemView.text_charity_time.text =
            "时间：${TimeUtils.millis2String(
                itemData.start_time * 1000, "yyyy-MM-dd HH:mm"
            )} - ${TimeUtils.millis2String(
                itemData.end_time * 1000, "yyyy-MM-dd HH:mm"
            )}"
        itemView.text_charity_state.text =
            if (itemData.is_time_cancel == 1) "报名中" else "已结束"
        itemView.text_charity_state.isEnabled =
            itemData.is_time_cancel == 1

        LogUtils.e("============${itemData.is_time_cancel}")
    }
}